fit_pandas_GUI()¶You can try this notebook live by lauching it in Binder.This can take a while to launch, be patient.
.
First we import pandas and pandas_GUI and then load some data to fit.
import pandas as pd
from pandas_GUI import *
df = pd.read_csv('DataSets/ethaneIRabs.csv.gz')
Then make a quick plot using plot_pandas_GUI() to see what the data looks like. See the Pandas GUI Website for examples and documentation on using the plotting GUI.
# CODE BLOCK generated using plot_pandas_GUI(). See https://github.com/JupyterPhysSciLab/jupyter_Pandas_GUI.
from plotly import graph_objects as go
Figure_1 = go.FigureWidget(layout_template="simple_white")
scat = go.Scatter(x = df['1/CM'], y = df['Absorbance'],
mode = 'lines', name = 'Absorbance',)
Figure_1.add_trace(scat)
Figure_1.update_xaxes(title= '1/cm', mirror = True)
Figure_1.update_yaxes(title= 'Absorbance', mirror = True)
Figure_1.update_layout(title = 'Figure_1', template = 'simple_white')
Figure_1.show()
# Force save widget states so that graph will still be
# available when notebook next opened in trusted state.
JPSLUtils.OTJS('Jupyter.actions.call("widgets:save-with-widgets");')
Figure 1: IR absorbance spectrum of ethane.
All three of the bands have multiple overlapping peaks in them, so will only be fit poorly. However, they are adequate to illustrate the procedure.
Start by launching the fitting GUI by putting the command fit_pandas_GUI() in an empty cell and running the cell.
the data set and a name for the trace is set. See the screenshot below.

"GaussianModel" was chosen from the popup menu. Gaussian fits work poorly if they do not start with a good guess for the peak center. In this case we will fit to the middle peak. Thus, 1500 was entered for the center location. *Note: that it is necessary to click outside the box the number was entered in to set the value.

the range of the data to fit to was chosen to ignore the other two peaks. Consecutive pairs of selected points starting with the lowest data index number define each range. Points can be deselected by holding down the ctrl key while clicking on a point.
The Extend fitted function plot box was also checked so that the fit is shown in the ignored regions.
See the screenshot below.

labels for the X and Y axis were input and the Display Mirror Axes box was checked.

the final checks were done and then the 'Do Fit' button was clicked, closing the GUI and running the code in the cell below to perform the fit and display the results.

# CODE BLOCK generated using fit_pandas_GUI(). See https://github.com/JupyterPhysSciLab/jupyter_Pandas_GUI.
# Imports (no effect if already imported)
import numpy as np
import lmfit as lmfit
import round_using_error as rue
import copy as copy
from plotly import graph_objects as go
from IPython.display import HTML
# Define data and trace name
Xvals = df["1/CM"]
Yvals = df["Absorbance"]
tracename = "Absorbance"
# Define error (uncertainty)
Yerr = df["Absorbance"]*0 + 1
# Define the fit model, initial guesses, and contraints
fitmod = lmfit.models.GaussianModel()
fitmod.set_param_hint("amplitude", vary = True)
fitmod.set_param_hint("center", vary = True, value = 1500.0)
fitmod.set_param_hint("sigma", vary = True, min = 0.0)
# Define fit ranges
Yfiterr = copy.deepcopy(Yerr) # ranges not to fit = np.inf
Xfitdata = copy.deepcopy(Xvals) # ranges where fit not displayed = np.nan
Yfiterr[0:612] = np.inf
Xfitdata[0:612] = np.nan
Yfiterr[1917:3571] = np.inf
Xfitdata[1917:3571] = np.nan
# Do fit
Fit_1 = fitmod.fit(Yvals, x=Xvals, weights = 1/Yfiterr, scale_covar = True, nan_policy = "omit")
# Calculate residuals (data - fit) because lmfit
# does not calculate for all points under all conditions
resid = []
for i in range(0,len(Fit_1.data)):
resid.append(Fit_1.data[i]-Fit_1.best_fit[i])
# Plot Results
Fit_1_Figure = go.FigureWidget(layout_template="simple_white")
Fit_1_Figure.update_layout(title = "Fit_1_Figure")
Fit_1_Figure.set_subplots(rows=2, cols=1, row_heights=[0.2,0.8], shared_xaxes=True)
scat = go.Scatter(y=resid,x=Xvals, mode="markers",name = "residuals")
Fit_1_Figure.update_yaxes(title = "Residuals", row=1, col=1, zeroline=True, zerolinecolor = "lightgrey", mirror = True)
Fit_1_Figure.update_xaxes(row=1, col=1, mirror = True)
Fit_1_Figure.add_trace(scat,col=1,row=1)
scat = go.Scatter(x=Xvals, y=Yvals, mode="markers", name=tracename)
Fit_1_Figure.add_trace(scat, col=1, row = 2)
Fit_1_Figure.update_yaxes(title = "Absorbance", row=2, col=1, mirror = True)
Fit_1_Figure.update_xaxes(title = "1/cm", row=2, col=1, mirror = True)
scat = go.Scatter(y=Fit_1.best_fit, x=Xvals, mode="lines", line_color = "black", name="extrapolated",line_dash="dash")
Fit_1_Figure.add_trace(scat, col=1, row=2)
scat = go.Scatter(y=Fit_1.best_fit,x=Xfitdata, mode="lines", name="fit", line_color = "black", line_dash="solid")
Fit_1_Figure.add_trace(scat,col=1,row=2)
Fit_1_Figure.show()
# Display best fit equation
ampstr = ''
centstr = ''
sigmastr = ''
for k in Fit_1.params.keys():
if Fit_1.params[k].vary:
paramstr = r'(\color{red}{'+rue.latex_rndwitherr(Fit_1.params[k].value,
Fit_1.params[k].stderr,
errdig=1, lowmag=-3)+'})'
else:
paramstr = r'\color{blue}{'+str(Fit_1.params[k].value,
)+'}'
if k == 'amplitude':
ampstr = paramstr
if k == 'center':
centstr = paramstr
if k == 'sigma':
sigmastr = paramstr
fitstr = r'$$fit = \frac{'+ampstr+'}{'+sigmastr+r'\sqrt{2\pi}}\exp \left( \frac{-\left[x-'+centstr+r'\right]^2}{2'+sigmastr+r'^2}\right)$$'
captionstr = '<p>Use the command <code>Fit_1</code> as the last line of a code cell for more details.</p>'
display(HTML(fitstr+captionstr))
Use the command Fit_1 as the last line of a code cell for more details.
Figure 2: The results of the fit. Notice that the cubic polynomial was a very good choice. All the parameters appear to have three or four significant figures based on the uncertainties. Zooming in around zero on the residuals shows how well this flattens the baseline. Despite the good baseline correction the spectrometer or sampling methods really need fixing...
In addition to trying it below if this is a live notebook, you can look at the other examples listed in the Pandas GUI website.
If you are running this notebook live in binder you can try it here by running the first cell to import the tools and load the data. Then run the cell below to create the fit GUI. Note: You may want to expand the collapsed instructions to learn more about each tab.
fit_pandas_GUI()